// pages/around/around.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iconList: [{
      icon: 'shop',
      color: 'red',
      badge: 0,
      name: '餐饮',
      amap:'050000'
    }, {
      icon: 'record',
      color: 'orange',
      badge: 0,
      name: '风景',
      amap:'110000'
    }, {
      icon: 'read',
      color: 'yellow',
      badge: 0,
      name: '科教',
      amap:'140000'
    }, {
      icon: 'service',
      color: 'olive',
      badge: 0,
      name: '生活服务',
      amap:'070000'
    }, {
      icon: 'safe',
      color: 'cyan',
      badge: 0,
      name: '医疗',
      amap:'090000'
    }, {
      icon: 'goods',
      color: 'blue',
      badge: 0,
      name: '购物',
      amap:'060000'
    }],
    gridCol:3,
    skin: false,
    location:'',
    geo_address:'',
    inputValue:'',
    list:[]
  },
  // showModal(e) {
  //   this.setData({
  //     modalName: e.currentTarget.dataset.target
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this
    // 1. 获取当前位置--经纬度
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log("获取当前位置--经纬度:",res)
        that.setData({
          location:longitude+','+latitude
        })
        // 2. 高德API获取结构化地址
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', //仅为示例，并非真实的接口地址
          data: {
            key: '2b8e8119dc262a8e1af2f57d9a1552f3',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log("高德API获取结构化地址:",res.data)
            var component = res.data.regeocode.addressComponent
            that.setData({
              geo_address:component.city+' '+component.district+' '+component.township
            })
            console.log(that.data.geo_address)
          }
        })
      }
    })
  },
  nav_detail(e){
    console.log(e.currentTarget.dataset.name)
    let title = e.currentTarget.dataset.name
    let amap = e.currentTarget.dataset.amap
    let location = this.data.location
    wx.navigateTo({
      url: '/pages/around_detail/around_detail?detail_title='+title + '&amap='+amap+'&location='+location,
    })
  },
  bindKeyInput(e){
    this.setData({
      inputValue:e.detail.value
    })
  },
  bindReplaceInput(){
    let that = this
    console.log(that.data.inputValue)
    wx.request({
      url: 'https://restapi.amap.com/v3/place/around?',
      data: {
        key: '2b8e8119dc262a8e1af2f57d9a1552f3',
        location: that.data.location,
        keywords: that.data.inputValue
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log('中餐厅', res)
        that.setData({
          list: res.data.pois
        })
        console.log('中餐厅', that.data.list)
      }
    })
  },
})